﻿using DevExpress.XtraEditors;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.门店出入库.Service;
using DXApplicationTangche.Utils;
using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.门店出入库
{
    public partial class Form流转码 : DevExpress.XtraEditors.XtraForm
    {
        private List<StockInventoryDao> stockInventoryDaos = new List<StockInventoryDao>();
        public Form流转码()
        {
            InitializeComponent();
        }


        private void simpleButton打印流转贴_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                String stockBarcode = 出入库Service.gerenateStockBarcode();
                this.print流转码(stockBarcode);
            }
        }
        private void print流转码(String stockBarcode93)
        {
            //留给销售的大吊牌
            //根据类型获取不同模板
            String filePath流转码 = "stockBarcode93.btw";
            String fileName流转码 = Path.GetFileName(filePath流转码);
            //根据相对路径判断模板是否存在
            //不存在则下载进行打印
            if (!System.IO.File.Exists(@"tag\" + fileName流转码))
            {
                FileUtil.DownloadPicture(URL.图片url + URL.打印模板路径 + filePath流转码, @"tag\" + fileName流转码, -1);
            }
            String str = System.AppDomain.CurrentDomain.BaseDirectory;
            Engine btEngine = new Engine();
            btEngine.Start();
            #region print补打大吊牌
            LabelFormatDocument btFormat = btEngine.Documents.Open(str + @"tag\" + fileName流转码);
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "Microsoft Print to PDF";
            btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 1;
            //对BTW模版相应字段进行赋值 
            btFormat.SubStrings["stockBarcode93"].Value = stockBarcode93;

            Messages messages;
            int waitout2 = 10000; // 10秒 超时 
            Result nResult2 = btFormat.Print("流转码" + stockBarcode93, waitout2, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
            #endregion
        }

        private void textBox成衣码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox流转码.Text))
                {
                    MessageBox.Show("请先扫描流转码");
                    this.ActiveControl = textBox流转码;
                    this.textBox成衣码.Clear();
                }
                if (!String.IsNullOrWhiteSpace(this.textBox成衣码.Text))
                {
                    try
                    {
                        //  入库
                        StockInventoryDao stockInventoryDao =
                            出入库Service.barcodeStockIn(this.textBox流转码.Text.Trim(), this.textBox成衣码.Text.Trim());
                        try
                        {
                            if (!String.IsNullOrEmpty(stockInventoryDao.size))
                            {
                                stockInventoryDao.size = stockInventoryDao.size.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
                            }
                        }
                        catch { }
                        //  gridcontrol显示
                        //if (!String.IsNullOrEmpty(stockInventoryDao.barcode))
                        //{
                        //    if (this.stockInventoryDaos.Count > 0)
                        //    {
                        //        if (!this.stockInventoryDaos.Exists(dto => dto.barcode.Equals(stockInventoryDao.barcode)))
                        //        {
                        //            this.stockInventoryDaos.Add(stockInventoryDao);
                        //        }
                        //    }
                        //    else
                        //    {
                        this.stockInventoryDaos.Add(stockInventoryDao);
                        //    }
                        //}
                        this.gridControl入库.DataSource = this.stockInventoryDaos;
                        this.gridView入库.RefreshData();
                        this.gridView入库.BestFitColumns();
                        this.textBox流转码.Clear();
                        this.textBox成衣码.Clear();
                        this.ActiveControl = this.textBox流转码;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void textBox流转码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.ActiveControl = this.textBox成衣码;
            }
        }

        private void textBox出库扫流转码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                StockInventoryDao stockInventoryDao出库 =
                出入库Service.barcodeStockOut(this.textBox出库扫流转码.Text.Trim());
                if (!String.IsNullOrEmpty(stockInventoryDao出库.stockbarcode))
                {
                    MessageBox.Show("出库成功");
                    this.textBox出库扫流转码.Clear();
                }
                else
                {
                    MessageBox.Show("出库失败");
                }
            }
        }
    }
}